package com.ruoyi.cderp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblTakeStockSheetDetail;
import com.ruoyi.cderp.mapper.TblTakeStockSheetMapper;
import com.ruoyi.cderp.domain.TblTakeStockSheet;
import com.ruoyi.cderp.service.ITblTakeStockSheetService;

/**
 * 库存盘点单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-14
 */
@Service
public class TblTakeStockSheetServiceImpl implements ITblTakeStockSheetService 
{
    @Autowired
    private TblTakeStockSheetMapper tblTakeStockSheetMapper;

    /**
     * 查询库存盘点单
     * 
     * @param id 库存盘点单主键
     * @return 库存盘点单
     */
    @Override
    public TblTakeStockSheet selectTblTakeStockSheetById(String id)
    {
        return tblTakeStockSheetMapper.selectTblTakeStockSheetById(id);
    }

    /**
     * 查询库存盘点单列表
     * 
     * @param tblTakeStockSheet 库存盘点单
     * @return 库存盘点单
     */
    @Override
    public List<TblTakeStockSheet> selectTblTakeStockSheetList(TblTakeStockSheet tblTakeStockSheet)
    {
        return tblTakeStockSheetMapper.selectTblTakeStockSheetList(tblTakeStockSheet);
    }

    /**
     * 新增库存盘点单
     * 
     * @param tblTakeStockSheet 库存盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblTakeStockSheet(TblTakeStockSheet tblTakeStockSheet)
    {
        tblTakeStockSheet.setCreateTime(DateUtils.getNowDate());
        int rows = tblTakeStockSheetMapper.insertTblTakeStockSheet(tblTakeStockSheet);
        insertTblTakeStockSheetDetail(tblTakeStockSheet);
        return rows;
    }

    /**
     * 修改库存盘点单
     * 
     * @param tblTakeStockSheet 库存盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblTakeStockSheet(TblTakeStockSheet tblTakeStockSheet)
    {
        tblTakeStockSheet.setUpdateTime(DateUtils.getNowDate());
        tblTakeStockSheetMapper.deleteTblTakeStockSheetDetailBySheetId(tblTakeStockSheet.getId());
        insertTblTakeStockSheetDetail(tblTakeStockSheet);
        return tblTakeStockSheetMapper.updateTblTakeStockSheet(tblTakeStockSheet);
    }

    /**
     * 批量删除库存盘点单
     * 
     * @param ids 需要删除的库存盘点单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblTakeStockSheetByIds(String[] ids)
    {
        tblTakeStockSheetMapper.deleteTblTakeStockSheetDetailBySheetIds(ids);
        return tblTakeStockSheetMapper.deleteTblTakeStockSheetByIds(ids);
    }

    /**
     * 删除库存盘点单信息
     * 
     * @param id 库存盘点单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblTakeStockSheetById(String id)
    {
        tblTakeStockSheetMapper.deleteTblTakeStockSheetDetailBySheetId(id);
        return tblTakeStockSheetMapper.deleteTblTakeStockSheetById(id);
    }

    /**
     * 新增库存盘点单明细信息
     * 
     * @param tblTakeStockSheet 库存盘点单对象
     */
    public void insertTblTakeStockSheetDetail(TblTakeStockSheet tblTakeStockSheet)
    {
        List<TblTakeStockSheetDetail> tblTakeStockSheetDetailList = tblTakeStockSheet.getTblTakeStockSheetDetailList();
        String id = tblTakeStockSheet.getId();
        if (StringUtils.isNotNull(tblTakeStockSheetDetailList))
        {
            List<TblTakeStockSheetDetail> list = new ArrayList<TblTakeStockSheetDetail>();
            for (TblTakeStockSheetDetail tblTakeStockSheetDetail : tblTakeStockSheetDetailList)
            {
                tblTakeStockSheetDetail.setSheetId(id);
                list.add(tblTakeStockSheetDetail);
            }
            if (list.size() > 0)
            {
                tblTakeStockSheetMapper.batchTblTakeStockSheetDetail(list);
            }
        }
    }
}
